package br.cefet.farmaciaonline.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.cefet.farmaciaonline.connection.ConnectionFactory;
import br.cefet.farmaciaonline.vo.Medico;
import br.cefet.farmaciaonline.vo.Paciente;


public class MedicoDAO {
	Connection con = null;
	PreparedStatement ptmt = null;
	ResultSet rs = null;
	private static MedicoDAO medicoDao ;
	
	private Connection getConnection() throws SQLException {
		Connection conn;
		conn = ConnectionFactory.getInstance().getConnection();
		return conn;
	}

	public static MedicoDAO getInstance(){ 
		
		if (medicoDao == null){
			medicoDao = new MedicoDAO();
		}
		
		return medicoDao;
		
	}
	
	public boolean inserir(Medico medico) {
		 boolean result = false;
			
			try{
				Connection conn = getConnection();		              
				if(conn != null)  {
					String querystring = "INSERT INTO medico (cpf , crm, localTrabalho, userid, telefone) VALUES (? ,?, ? ,? ,?)";
					
					ptmt = conn.prepareStatement(querystring);
					ptmt.setString(1, medico.getCpf());
					ptmt.setString(2, medico.getCrm());
					ptmt.setString(3, medico.getLocalTrabalho());
					ptmt.setInt(4, medico.getUserid());
					ptmt.setString(5, medico.getTelefone());
					
					ptmt.executeUpdate();
				}
			}catch(Exception e) {
				e.printStackTrace();
			}	
				
			
			return result;
	}
	
	public void add(Paciente paciente) {

		try {
			String querystring = "INSERT INTO EMPLOYEE VALUES(?,?,?,?,?)";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			/*
			 * ptmt.setString(1, String.valueOf(System.currentTimeMillis()));
			 * ptmt.setString(2, paciente.getEmpName()); ptmt.setString(3,
			 * paciente.getEmpAddr()); ptmt.setString(4,
			 * paciente.getEmpEmail()); ptmt.setString(5,
			 * paciente.getEmpPhone());
			 */
			ptmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public void update(Medico medico) {

		try {
			String querystring = "UPDATE medico SET cpf=?,crm=?,"
					+ "localtrabalho=?,telefone=? WHERE userid=?";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);

			
			 ptmt.setString(1, medico.getCpf()); 
			 ptmt.setString(2, medico.getCrm());
			 
			 ptmt.setString(3, medico.getLocalTrabalho()); 
			 ptmt.setString(4, medico.getTelefone());
			 ptmt.setInt(5, medico.getUserid());
			 
			ptmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public void delete(String employeeId) {

		try {
			String querystring = "DELETE FROM EMPLOYEE WHERE EMPID=?";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			ptmt.setString(1, employeeId);
			ptmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public List<Paciente> findAll() {
		List<Paciente> pacientes = new ArrayList<Paciente>();
		Paciente paciente = null;
		try {
			String querystring = "SELECT * FROM EMPLOYEE";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			rs = ptmt.executeQuery();
			while (rs.next()) {
				paciente = new Paciente();
				/*
				 * employeeBean.setEmpId(rs.getString(1)); Trocar por paciente
				 * employeeBean.setEmpName(rs.getString(2));
				 * employeeBean.setEmpAddr(rs.getString(3));
				 * employeeBean.setEmpEmail(rs.getString(4));
				 * employeeBean.setEmpPhone(rs.getString(5));
				 */

				pacientes.add(paciente);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return pacientes;
	}

	public Medico getById(int id) {

		Medico medico = null;
		try {
			String querystring = "SELECT * FROM medico WHERE userid=?";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			ptmt.setInt(1, id);
			rs = ptmt.executeQuery();
			if (rs.next()) {
				 medico=new Medico();
				 medico.setCpf(rs.getString(1));
				 medico.setCrm(rs.getString(2));
				 medico.setLocalTrabalho(rs.getString(3));
				 medico.setUserid(rs.getInt(4));
				 medico.setTelefone(rs.getString(5));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return medico;
	}

	public static void main(String[] args) {

		PacienteDAO employeeDAO = new PacienteDAO();
		System.out.println(employeeDAO.findAll().size());
	}
}